import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/common_search/bean/bean_common_search.dart';
import 'package:dk_flutter_module_sale/common/common_search/bean/event_bus_search_result.dart';
import 'package:dk_flutter_module_sale/common/common_search/widget/widget_common_search_bar.dart';
import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/config/route/common_widget_route.dart';
import 'package:dk_flutter_module_sale/sale/api/sale_out_api.dart';
import 'package:dk_flutter_module_sale/common/common_search/bean/bean_search_name.dart';

@ARoute(url: CommonWidgetRoute.SEARCH_SALE_NAME_LIST)
class SearchSaleNamePage extends CommonPage {
  final WidgetOption option;

  SearchSaleNamePage(this.option) : super();

  @override
  void onInitState(CommonPageBloc pageBloc) {
    pageBloc.switchPage(SearchSaleNameWidget(
        pageBloc, SecondSearchItem.fromJson(option.widgetParams), SearchNameBean.fromJson(option.widgetParams)));
  }
}

class SearchSaleNameWidget extends StatefulWidget {
  final CommonPageBloc pageBloc;
  final SecondSearchItem secondSearchItem;
  final SearchNameBean searchNameBean;

  SearchSaleNameWidget(this.pageBloc, this.secondSearchItem, this.searchNameBean);

  @override
  State<SearchSaleNameWidget> createState() {
    return SearchSaleNamePageState();
  }
}

class SearchSaleNamePageState extends State<SearchSaleNameWidget> {
  SearchNameListBloc searchNameListBloc;
  SaleNameListRequest request;

  @override
  void initState() {
    super.initState();
    request = SaleNameListRequest(keyWord: widget.secondSearchItem.displayName,useTo: widget.searchNameBean.useTo);
    searchNameListBloc = SearchNameListBloc();
    widget.pageBloc.eventBus
        .on<SearchResultEvent>()
        .listen((SearchResultEvent event) {
      request.keyWord = event.content ?? "";
      widget.pageBloc.cancelAllHttpAsync().then((f) {
        requestList();
      });
    });
    if (stringNotEmpty(widget.secondSearchItem.displayName)) {
      widget.pageBloc.eventBus
          .fire(SearchResultEvent(widget.secondSearchItem.displayName));
    }
  }

  void requestList() {
    widget.pageBloc.widgetHttpGet(
      widgetBloc: searchNameListBloc,
      path: SaleOutApi.searchSaleName,
      queryMap: request.toJson(),
      showLoading: false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DkAppBar(
        DKAppBarParams.withBackNoMenu(
          context,
          titleContent: "选择销售姓名",
        ),
      ),
      body: Column(
        children: <Widget>[
          CommonSearchBar(
            widget.pageBloc.eventBus,
            initContent: widget.secondSearchItem.displayName,
            placeHolder: "请输入销售姓名",
            isShowCancel: false,
          ),
          Expanded(
            child: BaseWidget(
              bloc: searchNameListBloc,
              handleWidgetResult: (dynamic result) {
                SaleNameList saleNameList = SaleNameList.fromJson(result);
                List<SaleNameItem> listData = saleNameList.list;
                return Container(
                  color: Colors.white,
                  child: DkVisibilityCompose(
                    VisibilityComposeOption.replaceWithOutSize(
                      (BuildContext childContext) {
                        return ListView.builder(
                          itemBuilder: (BuildContext listContext, int index) {
                            SaleNameItem item = listData[index];
                            return DkVisibilityCompose(
                              VisibilityComposeOption.gone(
                                (BuildContext itemContext) {
                                  return InkWell(
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Padding(
                                          padding: EdgeInsets.only(
                                              left: 24, top: 15, bottom: 17),
                                          child: DkText.simple(item?.name, 14,
                                              Color(0xff2A2A2A)),
                                        ),
                                        DkDivider(DividerOption.horizontal(
                                            indentStart: 15)),
                                      ],
                                    ),
                                    onTap: () {
                                      RouteUtil.pop(context,
                                          result: (SecondSearchItem()
                                                ..searchKey = widget.secondSearchItem.searchKey
                                                ..searchValue = item.id.toString()
                                                ..displayName = item.onlyName)
                                              .toJson());
                                    },
                                  );
                                },
                                visible: objNotEmpty(item) &&
                                    stringNotEmpty(item.name) &&
                                    stringNotEmpty(item.id.toString()),
                              ),
                            );
                          },
                          itemCount: listData.length,
                          shrinkWrap: true,
                          padding: EdgeInsets.only(bottom: 8),
                          controller: ScrollController(keepScrollOffset: false),
                        );
                      },
                      (BuildContext childContext) {
                        return LogicPage.emptyPage();
                      },
                      visible:
                          objNotEmpty(saleNameList) && listNotEmpty(listData),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

class SearchNameListBloc extends WidgetBloc {
  @override
  WidgetState get initialState => WidgetOffstageState();
}
